﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TowerController : MonoBehaviour
{
	#region 单例模型
	private static TowerController singleton;
	private TowerController() { }
	private void Awake() { singleton = this; }
	public static TowerController Singleton => singleton;
	#endregion

	public List<Tower> towersPrefabs = new List<Tower>();


	/// <summary>
	/// 升级防御塔
	/// </summary>
	public void UpdateTower(TowerBase towerBase) {
		Tower tower = towerBase.tower;
		if (tower.grade < 4) {
			DeleteTower(towerBase);
			CreateTower(tower.towerType, towerBase.transform, tower.grade + 1);
		} else {
			UIController.Singleton.CreateToast("已到达最高等级");
		}
	}


	public void DeleteTower(TowerBase towerBase) {
		Tower tower = towerBase.tower;
		Destroy(tower.gameObject);
	}


	/// <summary>
	/// 创建防御塔
	/// </summary>
	/// <param name="type"></param>
	/// <param name="target"></param>
	public void CreateTower(TowerType type, Transform target, int grade = 1) {
		Tower towerPrefab = GetTower(type, grade);  // 1级塔
		if (towerPrefab) {
			Tower tower = Instantiate(towerPrefab, target);
			target.GetComponent<TowerBase>().tower = tower;
			UIController.Singleton.ShowPopPanel(UIState.Update);
		}
	}

	/// <summary>
	/// 获取某个塔的方法
	/// </summary>
	/// <param name="type"></param>
	/// <param name="grade"></param>
	/// <returns></returns>
	public Tower GetTower(TowerType type, int grade) {
		foreach (Tower tower in towersPrefabs) {
			if (tower.towerType == type && tower.grade == grade)
				return tower;
		}
		return null;
	}
}
